<?php
/**
 * @Author:      zk
 * @DateTime:    18-7-23 上午11:14
 * @Description: 审批模型相关
 */

class OAApprovalModel extends Eloquent
{
    protected $table = 'oa_approval_model';

    /**
     * 状态数组
     * @var array
     */
    public static $statusArr = [
        1 => '启用',
        2 => '停用',
        3 => '删除',
    ];
    const STATUS_START = 1;
    const STATUS_STOP = 1;
    const STATUS_TYPE = 1;

    /**
     * @Author:      zk
     * @DateTime:    18-7-30 上午11:55
     * @Description: 根据审批模板id获取该审批模板编辑时是否被锁定
     * @param int $approval_model_id 审批模板id
     * @return Boolean  true:已被锁定 不可编辑; false:未被锁定 可编辑;
     */
    public static function getLockStatus($approval_model_id){
        $info = OAApprovalModel::where('id', $approval_model_id)
                ->select('lock_status')
                ->first();
        if($info){
            if($info->lock_status == 2){
                return TRUE;
            } else {
                return FALSE;
            }    
        } else {
            return TRUE;
        }
        
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-26 下午3:25
     * @Description: 根据审批模板id获取该审批模板是否已经被使用
     * @param int $approval_model_id 审批模板id
     * @return Boolean  true:已被使用; false:未被使用;
     */
    public static function getApprovalModelUsingHistory($approval_model_id){
        $info = OAApply::where('approval_model_id', $approval_model_id)
                ->select(['id', 'approval_model_id',])
                ->first();
        if($info){
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-27 上午9:45
     * @Description: 根据审批模板id获取该审批模板的详细信息
     * @param int $approval_model_id 审批模板id
     * @return object
     */
    public static function getApprovalModelInfoById($approval_model_id){
        $select_arr = [
            'oa_approval_model.id', 'oa_approval_model.approval_type_id', 'oa_approval_model.name', 'oa_approval_model.icon', 
            'oa_approval_model.status', 'oa_approval_model.approval_number_separator', 'oa_approval_model.desc', 
            'oa_approval_model.form_set', 'oa_approval_model.pc', 
            'oa_approval_model.wechat', 'oa_approval_model.email', 
            'oa_approval_type.name AS approval_type_name', 
        ];
        $info = OAApprovalModel::leftJoin('oa_approval_type', 'oa_approval_model.approval_type_id', '=', 'oa_approval_type.id')
                ->where('oa_approval_model.id', $approval_model_id)
                ->select($select_arr)
                ->first();
        return $info;
    }

    /**
     * 获取审批所有类型
     * @Author:      yyc
     * @DateTime:   18-7-24 下午2:04
     * @param  array  $statusComputer
     * @param  array  $statusWeChat
     * @param  int    $userId
     * @return mixed
     */
    public static function approvalInfo($userId, $statusComputer = [0, 1, 2], $statusWeChat = [0, 1, 2])
    {
        $selectArray = [
            'oa_approval_model.name as model_name', 'oa_approval_model.desc',
            'oa_approval_model.icon', 'oa_approval_model.pc', 'oa_approval_model.status',
            'oa_approval_model.wechat', 'oa_approval_type.name as type_name',
            'oa_approval_model.approval_type_id', 'oa_approval_model.id', 'oa.type as config',
            'oa_approval_type.status as typeStatus'
        ];
        $result = OAApprovalModel::select($selectArray)
            ->leftJoin('oa_approval_type', 'oa_approval_model.approval_type_id', '=','oa_approval_type.id')
            ->leftJoin('oa_approval_model_apply_config as oa', 'oa.approval_model_id', '=', 'oa_approval_model.id')
            ->whereRaw('oa.user_id = ' . $userId . ' OR oa.type = ' . self::STATUS_TYPE)
            ->havingRaw('typeStatus =' . self::STATUS_START . ' AND oa_approval_model.status = ' . self::STATUS_START)
            ->whereIn('wechat', $statusWeChat)
            ->groupBy('oa_approval_model.id')
            ->whereIn('pc', $statusComputer)
            ->get()
            ->toArray();
        $resultType = OAApprovalType::select('id')
            ->where('status', self::STATUS_START)
            ->get()
            ->toArray();
        $lastData = [];
        if ($resultType && $result) {
            foreach ($resultType as $type) {
                foreach ($result as $value) {
                    if ($type['id'] == $value['approval_type_id']) {
                        $lastData[$type['id']]['type'] = [
                            'typeName' => $value['type_name'],
                            'typeId' => $value['typeStatus'],
                        ];
                        $lastData[$type['id']]['info'][] = $value;
                    }
                }
            }
        }
        return array_values($lastData);
    }

    /**
     * 获取审批所有类型
     * @Author:      yyc
     * @DateTime:   18-7-24 下午2:04
     * @param       $userId  int 当前用户的ID
     * @return      mixed
     */
    public static function usuallyType($userId)
    {
        $selectArray = [
            'oa_approval_model.name as model_name', 'oa_approval_model.desc',
            'oa_approval_model.icon', 'oa_approval_model.pc', 'oa_approval_model.status',
            'oa_approval_model.wechat', 'oa_approval_type.name as type_name',
            'oa_approval_model.approval_type_id', 'oa_apply.created_at', 'oa_approval_model.id',
        ];

        $usuallyResult = OAApprovalModel::select($selectArray)
            ->leftJoin('oa_apply', 'oa_apply.approval_model_id', '=', 'oa_approval_model.id')
            ->leftJoin('oa_approval_type', 'oa_approval_type.id', '=', 'oa_apply.approval_type_id')
            ->orderBy('oa_apply.created_at', 'desc')
            ->where('oa_apply.created_by', $userId)
            ->where('oa_approval_model.status', '=', self::STATUS_START)
            ->where('oa_approval_type.status', '=', self::STATUS_START)
            ->get()
            ->toArray();
        $lastData = [];
        $flag = 0;
        $flagArray = [];
        if ($usuallyResult) {
            foreach ($usuallyResult as $value) {
                if (!in_array($value['id'],$flagArray) && $flag < 3) {
                    $lastData[] = $value;
                    $flagArray[] = $value['id'];
                    $flag++;
                }
            }
        }
        return $lastData;
    }

    /**
     * 获取审批所有类型
     * @Author:     yyc
     * @param       $status      array       审批类型的状态
     * @param       $page        int         当前页
     * @param       $pageSize    int         每页显示条数
     * @param       $type        array       审批的类型
     * @DateTime:   18-7-25     下午4:25
     * @return mixed
     */
    public static function searchInfo($status, $page, $pageSize, $type)
    {
        $selectArray = [
            'oa.type', 'oa_approval_model.name as modelName', 'oa_approval_type.name as typeName',
            'oa_approval_model.id as modelId', 'oa_approval_type.id as typeId', 'oa_approval_model.desc',
            'oa_approval_model.status as modelStatus', 'oa_approval_model.icon'
        ];
        $result = OAApprovalModel::select($selectArray)
            ->leftJoin('oa_approval_type', 'oa_approval_type.id', '=', 'oa_approval_model.approval_type_id')
            ->leftJoin('oa_approval_model_apply_config as oa', 'oa.approval_model_id', '=', 'oa_approval_model.id')
            ->whereIn('oa_approval_model.status', $status)
            ->groupBy('oa.approval_model_id')
            ->whereIn('oa_approval_type.status', [1, 2]);
        if (!empty($type)) {
            $result->where('oa_approval_type.id', '=', $type);
        }
        if (empty($result)) {
            return [];
        }

        $resultTemp = clone $result;

        $resultData = $result->skip($pageSize * ($page - 1))->take($pageSize)->get()->toArray();

        $count = '';
        $countResult = $resultTemp->get()->toArray();
        if (empty($countResult)) {
            return [];
        }

        if (empty($count)) {
            $count = count($countResult);
        }

        return [
            'count' => $count,
            'infoAll' => $resultData,
        ];
    }

    /**
     * 获取审批所有类型
     * @Author:      yyc
     * @DateTime:   18-7-25 下午4:55
     * @return mixed
     */
    public static function typeNeedAll()
    {
        $result = OAApprovalType::select(['id', 'name'])->whereIn('status', [1, 2])->get()->toArray();
        $result[] = ['id' => '', 'name' => '全部'];
        return $result;
    }

    /**
     * 修改审批模型的状态
     * @Author:      yyc
     * @DateTime:   18-7-25 下午5:30
     * @param       $status integer   状态
     * @param       $modelId integer  模型ID
     * @param       $userId integer  用户Id
     * @return mixed
     */
    public static function changeStatus($status, $modelId, $userId)
    {
        $result = OAApprovalModel::where('oa_approval_model.id', $modelId)
            ->update([
                'status' => $status,
                'created_by' => $userId,
                'created_at' => Common::num2DateTime(time()),
            ]);
        return $result;
    }

}
